import SwiftUI

struct ModelList: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var selectedGirlName: String? // 保存选择的女孩名字
    
    var body: some View {
        NavigationView {
            ZStack {
                MainView()
                VStack {
                    Back(
                        title: "Choice Model",
                        onBackTap: {
                            self.presentationMode.wrappedValue.dismiss()
                        },
                        onImageTap: {}
                    )
                    
                    ScrollView {
                        VStack(spacing: 20) { // 垂直间隔设置为20
                            ForEach(girls) { girl in
                                HStack {
                                    Image(girl.imageName)
                                        .resizable()
                                        .scaledToFit()
                                        .frame(width: 80, height: 80)
                                        .clipShape(Circle())
                                    
                                    VStack(alignment: .leading) {
                                        Text(girl.name)
                                            .font(.headline)
                                            .foregroundColor(.white)
                                        Text("Age: \(girl.age)")
                                            .font(.subheadline)
                                            .foregroundColor(.white)
                                        Text("Personality: \(girl.personality)")
                                            .font(.subheadline)
                                            .foregroundColor(.white)
                                    }
                                    .padding(.leading, 10)
                                    
                                    Spacer()
                                    
                                    Button(action: {
                                        selectedGirlName = girl.name
                                    }) {
                                        Image(systemName: "chevron.right")
                                            .padding()
                                            .background(Color.blue)
                                            .foregroundColor(.white)
                                            .clipShape(Circle())
                                    }
                                }
                                .padding()
                                .background(Color("mainback")) // 背景颜色
                                .clipShape(RoundedRectangle(cornerRadius: 20)) // 圆角
                                .overlay(
                                    RoundedRectangle(cornerRadius: 20)
                                        .stroke(Color.white, lineWidth: 1) // 白色边框
                                )
                                .background(
                                    NavigationLink(
                                        //girlName: selectedGirlName ?? ""
                                        destination: ChatView(girlName:selectedGirlName ?? "Lily"),
                                        isActive: Binding(
                                            get: { selectedGirlName == girl.name },
                                            set: { isActive in
                                                if !isActive { selectedGirlName = nil }
                                            }
                                        )
                                    ) {
                                        EmptyView()
                                    }
                                )
                            }
                        }
                        .padding()
                    }
                    
                    Spacer()
                }
                .edgesIgnoringSafeArea(.bottom)
            }
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
        .navigationViewStyle(StackNavigationViewStyle())
    }
    
}

#Preview {
    ModelList()
}

// 数据模型
struct GirlModel: Identifiable {
    let id = UUID()
    let name: String
    let age: Int
    let personality: String
    let imageName: String
}

// 示例数据
let girls = [
    GirlModel(name: "Lily", age: 22, personality: "Friendly", imageName: "g1"),
    GirlModel(name: "Anna", age: 25, personality: "Creative", imageName: "g2"),
    GirlModel(name: "Sophie", age: 30, personality: "Adventurous", imageName: "g3"),
    GirlModel(name: "Emma", age: 24, personality: "Witty", imageName: "g4"),
    GirlModel(name: "Olivia", age: 29, personality: "Outgoing", imageName: "g5"),
    GirlModel(name: "Ava", age: 26, personality: "Charming", imageName: "g6"),
    GirlModel(name: "Isabella", age: 27, personality: "Intelligent", imageName: "g7"),
    GirlModel(name: "Mia", age: 23, personality: "Playful", imageName: "g8"),
    GirlModel(name: "Sophia", age: 28, personality: "Artistic", imageName: "g9"),
    GirlModel(name: "Charlotte", age: 31, personality: "Ambitious", imageName: "g10")
]
